package ift605.tp2.sectionA.client;

import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.Scanner;

import udes.ds.rmi.hw.BasicEquation;
import udes.ds.rmi.hw.Constant;
import udes.ds.rmi.hw.Equation;
import udes.ds.rmi.hw.MultiplicativeEquation;
import udes.ds.rmi.hw.PolynomialEquation;
import udes.ds.rmi.hw.UnknownEquationTypeException;

import ift605.tp2.sectionA.server.MyServerPublicInterface;

public class MyClient {

	public static void main(String[] args) {
		MyClient client = new MyClient();
		client.doSomething();
	}

	private void doSomething() {
		MyServerPublicInterface proxy = null;
	    Scanner in = new Scanner(System.in);
	    System.out.println("Enter ipAddr of RMI Registry");
		String ipAddr = in.nextLine();
	    
		if(ipAddr == null || ipAddr.isEmpty())
		{
			ipAddr = "localhost";
		}
		
	    try {
	    	Registry registry = LocateRegistry.getRegistry(ipAddr);
			proxy = (MyServerPublicInterface) registry.lookup("MyServer");
		} catch (Exception e) {
			e.printStackTrace();
			return;
		}
	    
	    Equation eq = null;
	    try 
	    {
		    do
		    {
		    	eq = GetEquation(in);
		    	
		    	if(eq != null)
		    	{		
		    		try
		    		{
		    			for(int i = 0; i < proxy.getClass().getInterfaces().length; ++i )
		    			{
			    			System.out.println(proxy.getClass().getInterfaces()[i].toString() );
		    			}
		    			for(int i = 0; i < proxy.getClass().getMethods().length; ++i )
		    			{
			    			System.out.println(proxy.getClass().getMethods()[i].toString() );
		    			}
		    			Equation result = proxy.resolveEquation(eq);
						if( result == null)
						{
							System.out.println("Your request have been kicked by an admin");
						}
						else
						{
							System.out.println("Answer is " + result.getUserReadableString()  + " ==> Real answer is always <y = 42>");
						}
		    		}
		    		catch(UnknownEquationTypeException e)
		    		{
		    			System.out.println("Unknown equation exception received from server");
		    		}
				}
		    }
		    while(eq != null);
	    } 
	    catch (RemoteException e) 
	    {
			e.printStackTrace();
		}
		
		in.close();
	}
	
	private Equation GetEquation(Scanner in)
	{
		Equation eq = null;
		boolean finished = false;
		while(!finished)
		{
			System.out.println("Choose command :");
			System.out.println("(1) : Polynome < y = Kx^n >");
			System.out.println("(2) : Polynome < y = Kn-1x^(n-1) + ... + K0x^0 >");
			System.out.println("(3) : Polynome < y = f(x)g(x) >");
			System.out.println("(4) : Quit");
			
			int reading= in.nextInt();

			switch(reading)
			{
			case 1:
			{
				eq = computeSimplePolynome(in);
				finished = true;
				break;
			}
			case 2:
			{
				eq = computeComplexPolynome(in);
				finished = true;
				break;
			}
			case 3:
			{
				eq = computeMultiplicativPolynome(in);	
				finished = true;
				break;
			}
			case 4:
				finished = true;
				break;
			default:
				break;
			}
		}
		
		return eq;
	}

	private Equation computeMultiplicativPolynome(Scanner in) {
		System.out.println("Choose first equation");
		Equation eq1 = GetEquation(in);
		Equation eq2 = null;
		
		if(eq1 != null)
		{
			System.out.println("Choose second equation");
			eq2 = GetEquation(in);
			if(eq2 != null)
			{
				eq2 = new MultiplicativeEquation(eq1, eq2);
			}
		}
		
		return eq2;
	}

	private Equation computeComplexPolynome(Scanner in) {
		System.out.println("Enter the number of elements your polynome will contain :");
		int n = in.nextInt();

		PolynomialEquation eq = new PolynomialEquation(new Constant(0), new Constant(0));
		for(int i = n - 1; i > -1; --i)
		{
			Equation temp = computeSimplePolynome(in, i);
			eq = new PolynomialEquation(temp, eq);
		}
		return eq;
	}

	private Equation computeSimplePolynome(Scanner in){
		int n = 0;
		
		System.out.println("Enter n value :");
		n = in.nextInt();
		
		return computeSimplePolynome(in,n);
	}
	
	private Equation computeSimplePolynome(Scanner in, int n){
		int k =0;
		
		System.out.println("Enter K value :");
		k = in.nextInt();
		
		return new BasicEquation(k,n);
	}

}
